package com.seaside.park.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.seaside.park.bean.CarReception;
import com.seaside.park.bean.ParkLocker;
import com.seaside.park.mapper.CarReceptionMapper;
import com.seaside.park.mapper.ParkLockerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class CarReceptionService {

    @Autowired
    public CarReceptionMapper carReceptionMapper;

    @Autowired
    public ParkLockerMapper parkLockerMapper;

    public void updateLockStatus(int id, String lockStatus) {
        CarReception carReception = carReceptionMapper.selectById(id);
        if (carReception != null) {
            carReception.setLockStatus(lockStatus);
            carReception.setUpdatedAt(LocalDateTime.now());
            carReceptionMapper.updateById(carReception);
        }
    }

    public void returnCar(int id) {
        CarReception carReception = carReceptionMapper.selectById(id);
        if (carReception != null) {
            carReception.setCarStatus("已还车");
            carReception.setUpdatedAt(LocalDateTime.now());
            carReceptionMapper.updateById(carReception);
        }
    }

    public void updateCarStatus(int id, String carStatus) {
        CarReception carReception = carReceptionMapper.selectById(id);
        if (carReception != null) {
            carReception.setCarStatus(carStatus);
            carReception.setUpdatedAt(LocalDateTime.now());
            carReceptionMapper.updateById(carReception);
        }
    }

    public void moveCar(int id, String movedPhoto, String position, String movedNotes) {
        CarReception carReception = carReceptionMapper.selectById(id);
        if (carReception != null) {
            carReception.setMovedPhoto(movedPhoto);
            carReception.setPosition(position);
            carReception.setUpdatedAt(LocalDateTime.now());
            carReception.setMovedNotes(movedNotes);
            carReception.setCarStatus("已接车");
            carReceptionMapper.updateById(carReception);
            // 删除钥匙关联
            ParkLocker parkLocker = parkLockerMapper.selectOne(new QueryWrapper<ParkLocker>().eq("car_reception_id", id));
            parkLockerMapper.deleteById(parkLocker);
        }
    }

    public void useCar(int id, String targetPosition, LocalDateTime targetTime) {
        CarReception carReception = carReceptionMapper.selectById(id);
        if (carReception != null) {
            carReception.setTargetPosition(targetPosition);
            carReception.setCarStatus("待还车");
            carReception.setUpdatedAt(LocalDateTime.now());
            carReception.setTargetTime(targetTime);
            carReceptionMapper.updateById(carReception);
        }
    }

    public void cancelUseCar(int id) {
        CarReception carReception = carReceptionMapper.selectById(id);
        if (carReception != null) {
            carReception.setTargetPosition(null);
            carReception.setCarStatus("已泊位");
            carReception.setUpdatedAt(LocalDateTime.now());
            carReception.setTargetTime(null);
            carReceptionMapper.forceUpdateById(carReception);
        }
    }
}
